﻿using System;
using Microsoft.SharePoint;
using System.Diagnostics;
using SCCL2010;
using System.Web;
using Microsoft.Office.Server.UserProfiles;


namespace SCCL2010Server
{
    public static class UserProfies
    {
        public static string GetUserProfileProperty(SPSite oSite,int iUserID, HttpContext context, string cPropertyName)
        {
            Trace.WriteLine("Profile Property: " + cPropertyName);
            string cRetVal = "";
            Trace.AutoFlush = true;
            try
            {
                string siteURL = oSite.Url;
                SPSecurity.RunWithElevatedPrivileges(() =>
                {
                    try
                    {
                        using (SPSite site = new SPSite(siteURL))
                        {
                            Trace.WriteLine("Inside Getting Profile Property");
                            SPServiceContext serverContext = SPServiceContext.Current;
                            UserProfileManager profileManager = new UserProfileManager(serverContext);
                            SPUser spUser = site.RootWeb.Users.GetByID(iUserID);
                            if (spUser != null)
                            {
                                UserProfile profile = profileManager.GetUserProfile(spUser.LoginName);
                                cRetVal = profile[cPropertyName].Value.ToString();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.Message);
                        ErrorHandling.HandleError("UsersAndGroups.GetUserProfileProperty", "", ex);


                    }
                });
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                ErrorHandling.HandleError("UsersAndGroups.GetUserProfileProperty", "", ex);


            }
            Trace.WriteLine("Profile Property: " + cRetVal);

            return cRetVal;
        }

        /// <summary>
        /// Gets the UserProfileProperty based on the PropertyName and UserLogin.
        /// </summary>
        /// <param name="cUserLogin"></param>
        /// <param name="context"></param>
        /// <param name="cPropertyName"></param>
        /// <returns>return a string value representing the PropertyName based on the UserLogin and PropertyName.</returns>
        public static string GetUserProfileProperty(SPSite oSite,string cUserLogin, HttpContext context, string cPropertyName)
        {
            Trace.WriteLine("Profile Property: " + cPropertyName);
            string cRetVal = "";
            Trace.AutoFlush = true;
            try
            {
                string siteURL = oSite.Url;
                SPSecurity.RunWithElevatedPrivileges(() =>
                {
                    try
                    {
                        using (SPSite site = new SPSite(siteURL))
                        {
                            Trace.WriteLine("Inside Getting Profile Property");
                            SPServiceContext serverContext = SPServiceContext.Current;
                            UserProfileManager profileManager = new UserProfileManager(serverContext);
                            SPUser spUser = site.RootWeb.EnsureUser(cUserLogin);
                            if (spUser != null)
                            {
                                UserProfile profile = profileManager.GetUserProfile(spUser.LoginName);
                                cRetVal = profile[cPropertyName].Value.ToString();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ErrorHandling.HandleError("UsersAndGroups.GetUserProfileProperty", "", ex);


                    }
                });
            }
            catch (Exception ex)
            {
                ErrorHandling.HandleError("UsersAndGroups.GetUserProfileProperty", "", ex);


            }
            Trace.WriteLine("Profile Property: " + cRetVal);
            return cRetVal;
        }
    }
}
